﻿module Sustainalytics.Screening {

    import Common = Sustainalytics.Common;

    export class ScreeningToolServBackend implements IScreeningToolFactory {

        private $q: ng.IQService;
        private $http: ng.IHttpService;
        private $location: ng.ILocationService;

        private notificationFactory: Common.INotificationFactory;
        private staticObjects: Common.IStaticObjects;
        private $rootScope: ng.IScope;

        private urlBase: string;
        private currentFilterId: string;
        private static that: any;
        private accountId: string;
        private userId: string;

        constructor(
            $q: ng.IQService,
            $http: ng.IHttpService,
            $location: ng.ILocationService,
            notificationFactory: Common.INotificationFactory,
            staticObjects: Common.IStaticObjects,
            $rootScope: ng.IScope) {

            this.$q = $q;
            this.$http = $http;
            this.notificationFactory = notificationFactory;
            this.staticObjects = staticObjects;
            this.$location = $location;
            this.$rootScope = $rootScope;

            if (this.$rootScope['userInfo'] == null) {
                this.$rootScope['userInfo'] = angular.fromJson(localStorage.getItem('sust_userInfo'));
            }

            if (this.$rootScope['userInfo'] == null)
            {
                staticObjects.redirectToLogin();
                return;
            }
            else {
                this.accountId = this.$rootScope['userInfo'].accountId;
                this.userId = this.$rootScope['userInfo'].userId;
            }
            this.urlBase = staticObjects.screeningToolBaseUrl;
            ScreeningToolServBackend.that = this;
        }

        private showError(err) {
            var ctx = this;
            if (err && err['code'])
                ctx.notificationFactory.error('', ctx.staticObjects.getErrorMesageByStatusCode(err['code']));
        }

        private composeAnswersCriteriaTypeFive(arrAnswers: any): string {
            var result = "Minimum threshold is set to: ";

            if (arrAnswers.length === 1 && arrAnswers[0].score === -1)
                return "";

            arrAnswers.forEach((item, index) => {

                var text = item.text[0].toLowerCase() + item.text.substring(1);
                if (index == 0)
                    result += text;
                else
                    result += " or " + text;
            });

            result += ".";
            return result;
        }

        public composeAnswerTextCriteriaTypeFive(arrAnswers, score) {

            if (arrAnswers) {

                var list = arrAnswers;
                if (score != null) {
                    //filter  by score
                    list = arrAnswers.filter((elem) => { return elem.score == score; });
                }

                // sort by score 
                var sorted = list.sort((a, b) => { return a.score - b.score; });

                return this.composeAnswersCriteriaTypeFive(sorted);
            }
            return "";
        }

        public processCriteriaFiveAnswers(arrAnswers, textMap, values) {

            if (arrAnswers == null || arrAnswers.length == 0)
                return;

            // requirement:  sort  possible answers by  score, alphabetic code, keep text lower case  
            var sorted = arrAnswers.sort((a, b) => {

                if (a.score < b.score)
                    return -1;
                if (a.score > b.score)
                    return 1;

                if (a.code < b.code)
                    return -1;
                if (a.code > b.code)
                    return 1;

                return 0;
            });
            values.length = 0;
            sorted.forEach((item) => {

                var val = item.score;
                var obj = textMap[val];

                if (obj == null) {
                    textMap[val] = [];
                    values.push(item.score);
                }

                textMap[val].push(item);
            });
        }

        private composeAnswersCriteriaType25(arrAnswers: any): string {
            var result = "Minimum threshold is set to: ";

            if (arrAnswers.length === 1 && arrAnswers[0].score === -1)
                return "";

            arrAnswers.forEach((item, index) => {
                if (index == 0)
                    result += item.text;
                else
                    result += " or " + item.text;
            });

            result += ".";
            return result;
        }

        public composeAnswerTextCriteriaType25(arrAnswers, score) {

            if (arrAnswers) {

                var list = arrAnswers;
                if (score != null) {
                    //filter  by score
                    list = arrAnswers.filter((elem) => { return elem.score == score; });
                }

                // sort by score 
                var sorted = list.sort((a, b) => { return a.score - b.score; });

                return this.composeAnswersCriteriaType25(sorted);
            }
            return "";
        }

        public processCriteria25Answers(arrAnswers, textMap, values) {

            if (arrAnswers == null || arrAnswers.length == 0)
                return;

            // requirement:  sort  possible answers by  score, alphabetic code, keep text lower case  
            var sorted = arrAnswers.sort((a, b) => {

                if (a.score < 0)
                    return -1;
                if (b.score < 0)
                    return 1;
                if (a.score < b.score)
                    return 1;
                if (a.score > b.score)
                    return -1;
                return 0;
            });
            values.length = 0;
            sorted.forEach((item,index) => {

                //var val = item.score;
                var val = item.score == -1 ? item.score : index;
                var obj = textMap[val];

                if (obj == null) {
                    textMap[val] = [];
                    values.push(item.text);
                }

                textMap[val].push(item);
            });
        }

        public getCriteriaTabsAndTemplates(): ng.IPromise<any> {

            var def = this.$q.defer();
            var url = this.criteriaLevel1Url();
            var promise = this.$http.get(url, { params: { includeAllFields: true } });

            promise.success(
                data => {
                    def.resolve(data);
                }).error(
                err => {
                    this.notificationFactory.error('', this.staticObjects.getErrorMesageByStatusCode(err, err['code']));
                    def.reject();
                });

            return def.promise;
        }

        public listBaseCriteria(): ng.IPromise<BaseCriteria[]> {

            var def = this.$q.defer();
            var url = this.templateCriteriaUrl();
            var promise = this.$http.get(url, { params: { returnAllBaseCriteria: true } });

            promise.success(
                data => {
                    def.resolve(data);
                }).error(
                err => {
                    this.notificationFactory.error('', this.staticObjects.getErrorMesageByStatusCode(err, err['code']));
                    def.reject();
                });

            return def.promise;
        }

        public getPortfolios(filter, skip: number, take: number, orderBy: string) {
            {
                var ctx = this;
                var def = this.$q.defer();

                var options = new Common.ListOptions();
                options.skip = skip;
                options.take = take;
                options.orderBy = ["Name"];
                options.orderAscending = true;
                options.includeAllFields = false;
                options.getCount = false;

                var params = $.param({
                    isMyPortfolio: true,
                    namePortfolio: "",
                    sharingStatus: null,
                    isNameEqual: false,
                    accountId: this.accountId,
                    userId: this.userId,
                    listOptions: options
                });

                var url = "/portfolioserv/api/v1/portfolios?";
                var promise = this.$http.get(url, { params: params });

                promise.success(
                    data => {
                        def.resolve(data);
                    }).error(
                    err => {
                        this.notificationFactory.error('', this.staticObjects.getErrorMesageByStatusCode(err, err['code']));
                        def.reject();
                    });

                return def.promise;
            }
        }

        //#region URL
        //screeningtoolserv/api/v1/criterialevel1
        private criteriaLevel1Url(): string {
            return this.$location.protocol() + "://" + this.$location.host() + ":" + this.$location.port() + "/" + this.staticObjects.screeningToolServBaseUrl + "/criterialevel1";
        }
        
        //screeningtoolserv/api/v1/criterialevel1/{criteriaLevel1Id}/criterialevel2
        private criteriaLevel2Url(criteriaLevel1Id?: string): string {
            return this.$location.protocol() + "://" + this.$location.host() + ":" + this.$location.port() +
                this.staticObjects.screeningToolServBaseUrl + "/criterialevel1/" + criteriaLevel1Id;
        }
        
        //screeningtoolserv/api/v1/criterialevel1/{criteriaLevel1Id}/criterialevel2/{criteriaLevel2Id}/templatecriteria
        private templateCriteriaUrl(criteriaLevel1Id?: string, criteriaLevel2Id?: string, criteriaLevel3Id?: string, templateId?: string): string {
            return this.$location.protocol() + "://" + this.$location.host() + ":" + this.$location.port() +
                this.staticObjects.screeningToolServBaseUrl + "/criterialevel1/" + criteriaLevel1Id +
                "/criterialevel2/" + criteriaLevel2Id + "/criterialevel3/" + criteriaLevel3Id +  "/templatecriteria" + ((templateId) ? "/" + templateId : "");;
        }

        //screeningtoolserv/api/v1/filters/{id?}
        private filtersUrl(id?: string): string {
            return this.$location.protocol() + "://" + this.$location.host() + ":" + this.$location.port()
                + this.staticObjects.screeningToolServBaseUrl + "/filters/" + ((id) ? id : "");
        }
        
        //screeningtoolserv/api/v1/filters/{filterid}/criteria/{criteriaid?}
        private filterCriteriaUrl(filterid: string, id?: string): string {

            return this.$location.protocol() + "://" + this.$location.host() + ":" + this.$location.port() + this.staticObjects.screeningToolServBaseUrl
                + "/filters/" + filterid + "/criteria/" + ((id) ? id : "");
        }

        //screeningtoolserv/api/v1/filters/{filterid?}/criteria/{criteriaid?/collection/{collectionName}/content"}
        private filterCriteriaCollectionUrl(collName: string, filterid?: string, critId?: string): string {

            var urlComposed = this.$location.protocol() + "://" + this.$location.host() + ":" + this.$location.port() + this.staticObjects.screeningToolServBaseUrl
                + "/filters/" + ((filterid) ? filterid : null) + "/criteria/" + ((critId) ? critId : null) + "/collection/" + collName + "/content?";

            return urlComposed;
        }

        //screeningtoolserv/api/v1/runs/{filterid}
        private runUrl(filterid: string): string {

            return this.$location.protocol() + "://" + this.$location.host() + ":" + this.$location.port() + this.staticObjects.screeningToolServBaseUrl
                + "/runs/" + filterid;
        }

        //screeningtoolserv/api/v1/runs/{filterid}/results
        private runResultUrl(filterid: string): string {

            return this.$location.protocol() + "://" + this.$location.host() + ":" + this.$location.port() + this.staticObjects.screeningToolServBaseUrl
                + "/runs/" + filterid + "/results";
        }

        private productInvolvementUrl(symbol: string) {
            return this.$location.protocol() + "://" + this.$location.host() + ":" + this.$location.port() + "/productinvolvementserv/api/v1/piindicator/" + symbol + "/categories";
        }

        private universesUrl(id: string) {
            return this.$location.protocol() + "://" + this.$location.host() + ":" + this.$location.port() + "/universesserv/api/v1/universes/" + id;
        }
        //#endregion

        // updated functions

        //============ filters 
        public getCurrentFilter() {
            console.log("getCurrentFilter");
            var ctx = this;

            var promise = ctx.$http.get(ctx.filtersUrl(), { params: { CurrentFilter: true } });

            promise.success(data => { ctx.currentFilterId = (<SaveFilterDto>data).filterId; });
            return promise;

        }

        public listLastUsedFilters() {
            var ctx = ScreeningToolServBackend.that;
            return ctx.$http.get(ctx.filtersUrl(), { params: { lastUsed: true } });
        }

        public listUserFilters() {
            var ctx = ScreeningToolServBackend.that;
            var def = ctx.$q.defer();
            var promise = ctx.$http.get(ctx.filtersUrl(null), { params: { userFilters: true, standardFilters: true } });

            promise.success(data => {
                var filters = <Filter[]>data;
                if (filters.length > 0) {

                    var result = { userId: filters[0].associatedUserId, filters: [] };

                    filters.forEach(item => {

                        var obj = <ExtendedFilterDto>{ filter: <Filter>item, stats: <FilterStatsDto>item.stats };
                        result.filters.push(obj);
                    });
                    def.resolve(result);
                }
            });
            return def.promise;
        }

        public saveFilter(dto: SaveFilterDto) {
            var ctx = this;

            return this.$http({
                method: "PATCH",
                url: ctx.filtersUrl(null),
                data: [{ Verb: "PATCH", resourceid: dto.filterId, name: dto.name, shared: dto.shared }]
            });
        }

        public saveNewFilter(dto: SaveFilterDto) {
            return this.$http({
                method: "POST",
                url: this.filtersUrl(null),
                data: dto
            });
        }

        public loadSavedFilter(rootFilterId: string) {
            var ctx = this;
            return this.$http.get(ctx.filtersUrl(rootFilterId),
                { params: { isRootFilter: true, includeAllDetails: true } });
        }

        public SearchByCriteriaId(collName: string, term: any, skip: number, take: number) {
            var ctx = this;

            var options = new Common.ListOptions();
            options.skip = skip;
            options.take = take;
            options.orderBy = ["Name"];
            options.orderAscending = true;
            options.includeAllFields = false;
            options.getCount = false;

            var params = $.param({               
                collItemsNameLike: term,                
                listOptions: options
            });

            var urlExtended = ctx.filterCriteriaCollectionUrl(collName, "", "") + params;

            return this.$http.get(urlExtended);
        }

        public loadStandardFilter(standardFilter: StandardFilter) {
            var ctx = this;

            return this.$http.get(ctx.filtersUrl(), {
                params: { isStandardFilter: true, standardFilterType: standardFilter }
            });
        }

        public deleteCriteria(criteriaId: string) {
            var ctx = this;
            return this.$http({
                method: "PATCH",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: [{ Verb: "DELETE", resourceid: criteriaId }]
            });
        }

        public deleteAllCriteria(filterId) {
            var ctx = this;
            return this.$http({
                method: "PATCH",
                url: this.filterCriteriaUrl(filterId, null),
                data: [{ Verb: "DELETE", resourceid: null, deleteAll: true }]
            });
        }

        //=========== filter criteria

        public loadCriteriaTypeOne(criteriaTemplateId) {
            var ctx = this;
            return this.$http.get(ctx.templateCriteriaUrl(null, null, criteriaTemplateId), { params: { CriteriaType: 1, isTemplate: true } });
        }

        public addCriteriaTypeOne(criteriaTemplateId, minValue, maxValue) {
            var ctx = this;
            return this.$http({
                method: "POST",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: { CriteriaType: 1, minValue: minValue, maxValue: maxValue, templateid: criteriaTemplateId }
            });
        }

        public updateCriteriaTypeOne(criteriaId: string, minValue: number, maxValue: number) {
            var ctx = this;
            return this.$http({
                method: "PATCH",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: [{ Verb: "PATCH", resourceid: criteriaId, minValue: minValue, maxValue: maxValue, criteriaType: 1 }]
            });
        }


        public addCriteriaTypeTwo(criteriaTemplateId, score) {
            var ctx = this;
            return this.$http({
                method: "POST",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: { CriteriaType: 2, score: score, templateid: criteriaTemplateId }// FilterCriteriaDto
            });
        }

        public loadCriteriaTypeTwo(criteriaTemplateId) {
            var ctx = this;
            return this.$http.get(ctx.templateCriteriaUrl(ctx.currentFilterId, criteriaTemplateId), { params: { CriteriaType: 2, isTemplate: true } });
        }

        public updateCriteriaTypeTwo(criteriaId: string, score: number) {
            var ctx = this;
            return this.$http({
                method: "PATCH",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: [{ Verb: "PATCH", resourceid: criteriaId, score: score, criteriaType: 2 }]
            });
        }


        public loadCriteriaTypeThree(criteriaId, skip, take) {
            var ctx = this;
            return this.$http.get(ctx.filterCriteriaUrl(ctx.currentFilterId, criteriaId), { params: { CriteriaType: 3, skip: skip, take: take } });
        }

        public addCriteriaTypeThree(criteriaTemplateId, selectedItems) {
            var ctx = this;
            return this.$http({
                method: "POST",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: { CriteriaType: 3, selectedItems: selectedItems, templateid: criteriaTemplateId }
            });
        }

        public updateCriteriaTypeThree(criteria) {
            var ctx = this;
            return this.$http({
                method: "PATCH",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: [{ Verb: "PATCH", resourceid: criteria.id, selectedItems: criteria.selectedItems, criteriaType: 3 }]
            });
        }

        public getCriteriaTypeThreeCollectionItems(criteriaId, filter, loadMore, skip, take) {
            var ctx = this;
            return this.$http.get(ctx.templateCriteriaUrl(null, null, criteriaId), { params: { CriteriaType: 3, skip: skip, take: take, searchText: filter, loadMore: loadMore } });
        }


        public loadCriteriaTypeFourCategories(symbol) {
            return this.$http.get(this.productInvolvementUrl(symbol));
        }

        public addCriteriaTypeFour(criteriaTemplateId, piCategories) {
            return this.$http({
                method: "POST",
                url: this.filterCriteriaUrl(this.currentFilterId, null),
                data: { CriteriaType: 4, PICategories: piCategories, templateid: criteriaTemplateId }
            });
        }

        public loadCriteriaTypeFour(piFilterCriteriaId) {
            var ctx = this;
            return this.$http.get(this.filterCriteriaUrl(this.currentFilterId, piFilterCriteriaId), { params: { CriteriaType: 4 } });
        }

        public deleteCriteriaTypeFour(piFilterCriteriaId) {
            var ctx = this;
            return this.$http({
                method: "PATCH",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: [{ Verb: "PATCH", resourceId: piFilterCriteriaId, criteriaType: 4 }]// FilterCriteriaDto
            });
        }


        public loadTemplateCriteriaTypeFive(criteriaTemplateId) {
            var ctx = this;
            return this.$http.get(this.templateCriteriaUrl(null, null, criteriaTemplateId), { params: { CriteriaType: 5, isTemplate: true } });
        }

        public loadCriteriaTypeFive(criteriaId) {
            var ctx = this;
            return this.$http.get(ctx.filterCriteriaUrl(ctx.currentFilterId, criteriaId),
                { params: { CriteriaType: 5 } });
        }

        public addCriteriaTypeFive(criteriaTemplateId, score, templateNumber) {
            var ctx = this;
            return this.$http({
                method: "POST",
                url: ctx.filterCriteriaUrl(ctx.currentFilterId, null),
                data: { CriteriaType: 5, score: score, templateNumber: templateNumber, templateid: criteriaTemplateId }// FilterCriteriaDto
            });
        }

        public updateCriteriaTypeFive(criteriaId, score, templateNumber) {
            var ctx = this;
            return this.$http({
                method: "PATCH",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: [{ Verb: "PATCH", resourceid: criteriaId, score: score, templateNumber: templateNumber, criteriaType: 5 }]// FilterCriteriaDto
            });
        }

        public loadTemplateCriteriaType25(criteriaTemplateId) {
            var ctx = this;
            return this.$http.get(this.templateCriteriaUrl(null, null, criteriaTemplateId), { params: { CriteriaType: 25, isTemplate: true } });
        }

        public loadCriteriaType25(criteriaId) {
            var ctx = this;
            return this.$http.get(ctx.filterCriteriaUrl(ctx.currentFilterId, criteriaId),
                { params: { CriteriaType: 25 } });
        }

        public addCriteriaType25(criteriaTemplateId, score, templateNumber) {
            var ctx = this;
            return this.$http({
                method: "POST",
                url: ctx.filterCriteriaUrl(ctx.currentFilterId, null),
                data: { CriteriaType: 25, score: score, templateNumber: templateNumber, templateid: criteriaTemplateId }// FilterCriteriaDto
            });
        }

        public updateCriteriaType25(criteriaId, score, templateNumber) {
            var ctx = this;
            return this.$http({
                method: "PATCH",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: [{ Verb: "PATCH", resourceid: criteriaId, score: score, templateNumber: templateNumber, criteriaType: 25 }]// FilterCriteriaDto
            });
        }


        public addCriteriaTypeSix(criteriaTemplateId, minValue, maxValue) {
            var ctx = this;
            return this.$http({
                method: "POST",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: { CriteriaType: 6, minValue: minValue, maxValue: maxValue, templateid: criteriaTemplateId }
            });
        }

        public loadCriteriaTypeSix(criteriaTemplateId: string) {
            var ctx = this;
            return this.$http.get(this.templateCriteriaUrl(null, null, criteriaTemplateId), { params: { CriteriaType: 6, isTemplate: true } });
        }

        public updateCriteriaTypeSix(criteriaId: string, minValue: number, maxValue: number) {
            var ctx = this;
            return this.$http({
                method: "PATCH",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: [{ Verb: "PATCH", resourceid: criteriaId, minValue: minValue, maxValue: maxValue, criteriaType: 6 }]// FilterCriteriaDto
            });
        }


        public loadCriteriaTypeSeven(criteriaId, skip, take) {
            var ctx = this;
            return this.$http.get(ctx.filterCriteriaUrl(ctx.currentFilterId, criteriaId), { params: { CriteriaType: 7, skip: skip, take: take } });
        }

        public addCriteriaTypeSeven(criteriaTemplateId, selectedItems) {
            var ctx = this;
            return this.$http({
                method: "POST",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: { CriteriaType: 7, selectedItems: selectedItems, templateid: criteriaTemplateId }
            });
        }

        public updateCriteriaTypeSeven(criteria) {
            var ctx = this;
            return this.$http({
                method: "PATCH",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: [{ Verb: "PATCH", resourceid: criteria.id, selectedItems: criteria.selectedItems, criteriaType: 7 }]
            });
        }

        public getCriteriaTypeSevenCollectionItems(criteriaId, filter, loadMore, skip, take) {
            var ctx = this;
            return this.$http.get(ctx.templateCriteriaUrl(null, null, criteriaId), { params: { CriteriaType: 7, skip: skip, take: take, searchText: filter, loadMore: loadMore } });
        }

        public loadCriteriaTypeEight(criteriaId) {
            var ctx = this;
            return this.$http.get(ctx.filterCriteriaUrl(ctx.currentFilterId, criteriaId),
                { params: { CriteriaType: 8 } });
        }

        public addCriteriaTypeEight(criteriaTemplateId, score) {
            var ctx = this;
            return this.$http({
                method: "POST",
                url: ctx.filterCriteriaUrl(ctx.currentFilterId, null),
                data: { CriteriaType: 8, score: score, templateid: criteriaTemplateId }// FilterCriteriaDto
            });
        }

        public updateCriteriaTypeEight(criteriaId, score) {
            var ctx = this;
            return this.$http({
                method: "PATCH",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: [{ Verb: "PATCH", resourceid: criteriaId, score: score, criteriaType: 8 }]// FilterCriteriaDto
            });
        }

        public loadCriteriaType16(criteriaTemplateId) {
            var ctx = this;
            return this.$http.get(ctx.templateCriteriaUrl(null, null, criteriaTemplateId), { params: { CriteriaType: 16, isTemplate: true } });
        }

        public addCriteriaType16(criteriaTemplateId, minValue, maxValue) {
            var ctx = this;
            return this.$http({
                method: "POST",
                url: this.filterCriteriaUrl(ctx.currentFilterId, null),
                data: { CriteriaType: 16, minValue: minValue, maxValue: maxValue, templateid: criteriaTemplateId }
            });
        }

        //========== filter companies 
        public runSelectedCriteria(filterId: any, portfolioId: string, universeId: number, report: boolean, refreshOutput: boolean) {
            // this is get on companies
            var ctx = this;
            return this.$http({
                method: "PUT",
                url: this.runUrl(filterId),
                data: {
                    filterId: filterId, PortfolioId: portfolioId, UniverseId: universeId, Report: report, RefreshOutput: refreshOutput
                }
            });
        }

        public getResultOutput(filter: ResultOutputFilter, skip: number, take: number, orderBy: string, asc: boolean = true) {
            return this.$http.get(this.runResultUrl(filter.filterId), {
                params: {
                    isValid: filter.isValid,
                    portfolioId: filter.portfolioId,
                    skip: skip,
                    take: take,
                    orderBy: [orderBy],
                    orderAscending: asc,
                    getCount: true
                }
            });
        }

        public deleteResultOutput(filterId) {
            var ctx = this;
            console.log(this.runUrl(this.currentFilterId));
            return this.$http({
                method: "DELETE",
                url: this.runUrl(this.currentFilterId)
            });
        }

        public deleteFullFilter(filter) {
            var ctx = this;
            var def = this.$q.defer();

            var promise = this.$http({
                method: "PATCH",
                url: ctx.filtersUrl(null),
                data: [{ Verb: "DELETE", resourceid: filter.filterId, name: filter.name, shared: filter.shared }]
            });
            
            return promise;
        }

        public exportCurrentCriteria(filterId: string) {
            var ctx = this;
            return this.$http({ method: "POST", url: "screeningtoolserv/api/v1/export", data: filterId });
        }

        public loadUniverses() {
            return this.$http({
                method: "GET", url: this.universesUrl(this.accountId)
            }); 
        }

        public listTemplateCriteriaByLevel3Id(criteriaLevel3Id: string) {
            var ctx = this;
            return this.$http.get(ctx.templateCriteriaUrl(null, null, criteriaLevel3Id));
        }
    }

    var mod = angular.module(screeningToolModuleName);

    mod.service('screeningToolFactory',
        ['$q', '$http', '$location', 'notificationFactory', 'staticObjects', '$rootScope', ScreeningToolServBackend]);
}